package org.mili.ticketeer.ui.ticket;

import org.freixas.jcalendar.JCalendarCombo;
import org.mili.ticketeer.dto.Contact;
import org.mili.ticketeer.dto.Group;
import org.mili.ticketeer.dto.Identifieable;
import org.mili.ticketeer.dto.Priority;
import org.mili.ticketeer.dto.State;
import org.mili.ticketeer.dto.Ticket;
import org.mili.ticketeer.service.GroupService;
import org.mili.ticketeer.service.PriorityService;
import org.mili.ticketeer.service.ServiceException;
import org.mili.ticketeer.service.ServiceFactory;
import org.mili.ticketeer.service.StateService;
import org.mili.ticketeer.service.TicketService;
import org.mili.ticketeer.ui.FormElement;
import org.mili.ticketeer.ui.SimpleForm;
import org.mili.ticketeer.util.Log;

import javax.swing.*;
import java.awt.*;

/**
 * Created with IntelliJ IDEA.
 * User: papa
 * Date: 20.07.13
 * Time: 14:10
 * To change this template use File | Settings | File Templates.
 */
public class TicketDataForm extends SimpleForm<Ticket> {
    private TicketService ticketService = ServiceFactory.getService(TicketService.class);
    private GroupService groupService = ServiceFactory.getService(GroupService.class);
    private PriorityService priorityService = ServiceFactory.getService(PriorityService.class);
    private StateService stateService = ServiceFactory.getService(StateService.class);

    public TicketDataForm(String label) {
        super(FormMode.NORMAL, label, new FormElement[]{
                new FormElement(Identifieable.Fields.ID, new JTextField()),
                new FormElement(Ticket.Fields.CREATEDAT, new JCalendarCombo(
                        JCalendarCombo.DISPLAY_DATE | JCalendarCombo.DISPLAY_TIME, true)),
                new FormElement(Ticket.Fields.GROUP, new JComboBox<Group>()),
                new FormElement(Ticket.Fields.TITLE, new JTextField()),
                new FormElement(Ticket.Fields.PRIORITY, new JComboBox<Priority>()),
                new FormElement(Ticket.Fields.STATE, new JComboBox<State>()),
                new FormElement(Ticket.Fields.COMPLETEDAT, new JCalendarCombo(
                        JCalendarCombo.DISPLAY_DATE | JCalendarCombo.DISPLAY_TIME, true)),
                new FormElement(Ticket.Fields.COMMENT, new JTextField())
        });

        JTextField id = (JTextField) getFormElement(Identifieable.Fields.ID).getComponent();
        id.setEnabled(false);

        JComboBox priority = (JComboBox) getFormElement(Ticket.Fields.PRIORITY).getComponent();
        priority.setRenderer(new ListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                    boolean cellHasFocus) {
                Priority priority1 = (Priority) value;
                return new JLabel(String.format("%s (%s)", priority1.getName(), priority1.getPriority()));
            }
        });

        JComboBox group = (JComboBox) getFormElement(Ticket.Fields.GROUP).getComponent();
        group.setRenderer(new ListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                    boolean cellHasFocus) {
                Group group1 = (Group) value;
                return new JLabel(group1.getName());
            }
        });

        JComboBox state = (JComboBox) getFormElement(Ticket.Fields.STATE).getComponent();
        state.setRenderer(new ListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                                                          boolean cellHasFocus) {
                State state1 = (State) value;
                return new JLabel(String.format("%s (%s)", state1.getTitle(), state1.getToken()));
            }
        });

        try {
            group.setModel(new DefaultComboBoxModel(groupService.getGroups().toArray()));
            priority.setModel(new DefaultComboBoxModel(priorityService.getPriorities().toArray()));
            state.setModel(new DefaultComboBoxModel(stateService.getStates().toArray()));
        } catch (ServiceException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void save() throws ServiceException {
        formObject.setId(getInt(Identifieable.Fields.ID));
        formObject.setTitle(getString(Ticket.Fields.TITLE));
        formObject.setCreatedAt(getDate(Ticket.Fields.CREATEDAT));
        formObject.setCompletedAt(getDate(Ticket.Fields.COMPLETEDAT));
        formObject.setComment(getString(Ticket.Fields.COMMENT));
        formObject.setGroup(((Group) getObject(Ticket.Fields.GROUP)).getId());
        formObject.setPriority(((Priority) getObject(Ticket.Fields.PRIORITY)).getId());
        formObject.setState(((State) getObject(Ticket.Fields.STATE)).getId());
        ticketService.write(formObject);
    }

    @Override
    public void reset() {
        if (formObject.getId() > 0) {
            onSelectionChanged(formObject);
        } else {
            onSelectionChanged(new Contact());
        }
    }

    @Override
    public void cancel() {
        reset();
    }

    @Override
    public boolean validation() {
        Log.log(this, "validation", "true");
        return true;
    }

    @Override
    public void onSelectionChanged(Object element) {
        Log.log(this, "selectionChanged", "element: %s", element);
        if (element instanceof Ticket) {
            formObject = (Ticket) element;
            updateTextField(Identifieable.Fields.ID, String.valueOf(formObject.getId()));
            updateTextField(Ticket.Fields.TITLE, formObject.getTitle());
            updateTextField(Ticket.Fields.COMMENT, formObject.getTitle());
            try {
                updateComboBox(Ticket.Fields.GROUP, groupService.findById(formObject.getGroup()));
                updateComboBox(Ticket.Fields.PRIORITY, priorityService.findById(formObject.getPriority()));
                updateComboBox(Ticket.Fields.STATE, stateService.findById(formObject.getState()));
            } catch (ServiceException e) {
                e.printStackTrace();
            }
            updateCalendarCombo(Ticket.Fields.CREATEDAT, formObject.getCreatedAt());
            updateCalendarCombo(Ticket.Fields.COMPLETEDAT, formObject.getCompletedAt());
        }
        Log.log(this, "selectionChanged", "end");
    }

}
